/*==============================================================================
**                                  LINDBOARD
**                      Proprietary - Copyright (C) 2014
**                      www.facebook.com/lindboard
**------------------------------------------------------------------------------
** Supported MCUs      : MCU STM32F10x
** Supported Compilers : KeilC
**------------------------------------------------------------------------------
** File name         : audio.c
**  
** Module name       : Audio
**
** Summary: 
**
**= History ===================================================================
** 00.00.01  28/7/2014  Cuong Chu
**           - Creation
==============================================================================*/
/******************************************************************************/
/* INCLUSIONS                                                                 */
/******************************************************************************/
#include "stm32f10x.h"
/*******************************************************************************/
/* DEFINITION OF MACROS                                                        */
/*******************************************************************************/

/******************************************************************************/
/* DEFINITION OF CONSTANTS                                                    */
/******************************************************************************/
#define NULL_PTR                 ((void *)0)


/******************************************************************************/
/* DEFINITION OF LOCAL VARIABLE                                               */
/******************************************************************************/

/******************************************************************************/
/* DECLARATION OF LOCAL FUNCTIONS                                             */
/******************************************************************************/
/*******************************************************************************
** Function name: AUDIO_HWInit( void  )
** Description  : The function initializes audio hardware          
** Parameter    : none
** Return value : none
** Remarks      : global variables used, side effects
*******************************************************************************/
static void AUDIO_HWInit
(
  void
);
/******************************************************************************/
/* DEFINITION OF GLOBAL FUNCTIONS                                             */
/******************************************************************************/
/*******************************************************************************
** Function name: AUDIO_Init( void  )
** Description  : The function initializes audio module          
** Parameter    : none
** Return value : none
** Remarks      : global variables used, side effects
*******************************************************************************/
void AUDIO_Init
(
  void
)
{
  /* Init Hardware */
  AUDIO_HWInit();
}
/*******************************************************************************
** Function name: AUDIO_Out( void  )
** Description  : The function initializes audio hardware          
** Parameter    : - LEFT : value for left channel
**                - RIGHT: value for right channel
** Return value : none
** Remarks      : global variables used, side effects
*******************************************************************************/
void AUDIO_Out
(
  uint32_t LEFT,
  uint32_t RIGHT
)
{
  /* out data here */
  /* right data */
  DAC->DHR12R1 &= ~(0x0FFF);
  DAC->DHR12R1 |= (0x0FFF&RIGHT);
  /* left data */
  DAC->DHR12L1 &= ~(0xFFF0);
  DAC->DHR12L1 |= (0xFFF0&(LEFT<<4));
  /* software trigger DAC channel 1 to load data from the DAC_DHR1 register to
    the DAC_DOR1 register.
    This is bit reset by hardware.
  */
  DAC->SWTRIGR |= 1;
  /* nop */
 // asm("nop");    
}
/******************************************************************************/
/* DEFINITION OF LOCAL FUNCTIONS                                              */
/******************************************************************************/
/*******************************************************************************
** Function name: AUDIO_HWInit( void  )
** Description  : The function initializes audio hardware          
** Parameter    : none
** Return value : none
** Remarks      : global variables used, side effects
*******************************************************************************/
static void AUDIO_HWInit
(
  void
)
{
	/* Init DAC */ 
  /* enable clock DAC */
  RCC->APB1ENR  |= (1UL << 29);
  /* disabled DAC for channel 1
  Bit 12
  */
  DAC->CR &= ~(1UL<<12);
  /* DAC channel1 mask/amplitude selector 
  Bits 11:8
  0000: Unmask bit0 of LFSR/ Triangle Amplitude equal to 1
  0001: Unmask bits[1:0] of LFSR/ Triangle Amplitude equal to 3
  0010: Unmask bits[2:0] of LFSR/ Triangle Amplitude equal to 7
  0011: Unmask bits[3:0] of LFSR/ Triangle Amplitude equal to 15
  0100: Unmask bits[4:0] of LFSR/ Triangle Amplitude equal to 31
  0101: Unmask bits[5:0] of LFSR/ Triangle Amplitude equal to 63
  0110: Unmask bits[6:0] of LFSR/ Triangle Amplitude equal to 127
  0111: Unmask bits[7:0] of LFSR/ Triangle Amplitude equal to 255
  1000: Unmask bits[8:0] of LFSR/ Triangle Amplitude equal to 511
  1001: Unmask bits[9:0] of LFSR/ Triangle Amplitude equal to 1023
  1010: Unmask bits[10:0] of LFSR/ Triangle Amplitude equal to 2047
≥ 1011: Unmask bits[11:0] of LFSR/ Triangle Amplitude equal to 4095
  */
  DAC->CR &= ~(15UL<<8);
  DAC->CR |= (12<<8);
  /* DAC channel1 trigger selection
  Bits 5:3  
  These bits select the external event used to trigger DAC channel1
  000: Timer 6 TRGO event
  001: Timer 3 TRGO event in connectivity line devices, Timer 8 TRGO in high-density and
  XL-density devices
  010: Timer 7 TRGO event
  011: Timer 5 TRGO event
  100: Timer 2 TRGO event
  101: Timer 4 TRGO event
  110: External line9
  111: Software trigger
  */
  DAC->CR &= ~(7UL<<3);
  DAC->CR |= (7UL<<3);
  /*
    DAC channel1 output buffer disable
    Bit 1
    This bit set and cleared by software to enable/disable DAC channel1 output buffer.
    0: DAC channel1 output buffer enabled
    1: DAC channel1 output buffer disabled
  */
  DAC->CR |= (1UL<<1);
  /* 
  DAC channel1 enable
  Bit 0
  This bit set and cleared by software to enable/disable DAC channel1.
  0: DAC channel1 disabled
  1: DAC channel1 enabled
  */
  DAC->CR |= 1;
  /* configure DAC output pin at analog mode */ 
  /* PA4 */
  GPIOA->CRL &= ~(3<<16);
  GPIOA->CRL &= ~(3<<18);
}
/*****************************************************************************/
/* End Of File Copyright LINDBOARD 2014                                      */
/*****************************************************************************/
